#include"process.h"

#define LENGTH 101
#define LABLE '#'
void FlushProcess(double target , double current)
{
    double rate = (current/target)*100.0;
    
    //处理光标问题
    const static char* symb = "|/-\\";
    int len = strlen(symb);
    static int index = 0;
        
    //缓冲区、初始化、填数据
    char bar[LENGTH];
    memset(bar,'\0',sizeof(bar));
    //bar 中的# 数量为rate的整数部分
    int cnt = (int)rate;
    for(int i = 0;i<cnt;i++) bar[i]= LABLE;
    
    //打印
    printf("[%-100s][%.1lf%%][%c]\r",bar,rate,symb[index++]);
    index%=len;//处理越界情况
    fflush(stdout);//刷新
    
    //最后一次换行
    if(rate >= 100.0) printf("\n");
}

void Process()
{
    //[####][13%][|]
    char bar[LENGTH];
    memset(bar,'\0',sizeof(bar));
    const char* symb = "/|-\\";
    int len = strlen(symb);

    int cnt = 0;
    while(cnt < LENGTH)
    {
       // printf("%d\r",cnt);
        //printf("[%-100s][%d%%][%c]\r",bar,cnt,symb[cnt%len]);
        printf("[%-100s][%d%%][%c]\r",bar,cnt,symb[cnt%len]);
        //printf("[%-100s][%c]\r",bar,symb[cnt%len]);
        fflush(stdout);//刷新缓冲区
        bar[cnt]=LABLE;
        cnt++;
        usleep(10000);
    }
    printf("\n");
}
